package com.itlong.cloud.controller.sdkV1.old.serverClient;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.DTO.property.PropertyCloudIntercomSaveDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyEquipGetDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKEquipGetByPageDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKEquipGetPageDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKEquipSaveDTO;
import com.itlong.cloud.POJO.DTO.sdk.SDKUnitInfoSaveDTO;
import com.itlong.cloud.POJO.PO.ProjectPO;
import com.itlong.cloud.POJO.PO.PropertyRoleProjectPO;
import com.itlong.cloud.POJO.PO.TimeTaskAuthDeviceAddressInfoPO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipAddressVO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipGetVO;
import com.itlong.cloud.POJO.VO.property.PropertyEquipRecordGetVO;
import com.itlong.cloud.POJO.VO.property.PropertyProjectUnitSimpleInfoVO;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.enumerate.PropertyEquipTypeEnum;
import com.itlong.cloud.enumerate.PropertyPublicRoleEnum;
import com.itlong.cloud.enumerate.oldapp.Code;
import com.itlong.cloud.enumerate.oldapp.Message;
import com.itlong.cloud.equip.property.IPropertyDeviceAuthServiceClient;
import com.itlong.cloud.equip.property.IPropertyEquipServiceClient;
import com.itlong.cloud.equip.sdk.ISDKServerOldEquipServiceClient;
import com.itlong.cloud.project.property.IPropertyUnitInfoServiceClient;
import com.itlong.cloud.project.sdk.ISDKServerOldBuildServiceClient;
import com.itlong.cloud.project.sdk.ISDKServerOldProjectServiceClient;
import com.itlong.cloud.project.sdk.ISDKServerOldUnitServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.user.property.IPropertyHouseHoldInfoServiceClient;
import com.itlong.cloud.user.sdk.ISDKServerOldUserServiceClient;
import com.itlong.cloud.utils.encrypt.DesEncryptUtil;
import com.itlong.cloud.utils.node.SetListUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <desc>
 *      SDK服务端设备信息Controller
 * </desc>
 *
 * @createDate 2017/10/26
 */
@RestController
@RequestMapping("/jiabanjing/interface")
public class SDKServerOldEquipController {

    private static Logger LOG = LoggerFactory.getLogger(SDKServerOldEquipController.class);

    @Autowired
    ISDKServerOldUserServiceClient iSDKUserServiceClient;

    @Autowired
    ISDKServerOldProjectServiceClient iSDKProjectServiceClient;

    @Autowired
    ISDKServerOldBuildServiceClient iSDKBuildServiceClient;

    @Autowired
    ISDKServerOldUnitServiceClient iSDKServerOldUnitServiceClient;

    @Autowired
    ISDKServerOldEquipServiceClient iSDKServerEquipServiceClient;

    @Autowired
    IPropertyDeviceAuthServiceClient iPropertyDeviceAuthServiceClient;

    @Autowired
    IPropertyHouseHoldInfoServiceClient iPropertyHouseHoldInfoServiceClient;

    @Autowired
    IPropertyEquipServiceClient iPropertyEquipServiceClient;

    @Autowired
    IPropertyUnitInfoServiceClient iPropertyUnitInfoServiceClient;


    /**
     * <desc>
     *     SDK添加设备：电梯或门禁
     * <desc/>
     * @param       appid     应用接入凭证
     * @param    projectId  待添加房间的小区ID
     * @param    deviceUnique   修改设备的设备唯一码
     * @param    deviceJson   待添加设备Json数据
     * @return       Message   旧版标准格式化响应结果
     * @author Biao.R
     * @createDate 2017/11/14
     */
    @ResponseBody
    @RequestMapping("/server/device/add")
    @LoggerInfo(operateDesc = LogDescConstant.SDK_SERVER_ADD_FAMILY_LOG_DESC)
    public Message sdkAddDevice(String appid, String projectId, String deviceUnique, String deviceJson) {
        try {
            projectId = DesEncryptUtil.projectIdDesEncrypt(projectId);
            if (projectId == null) {
                return new Message(Code.APP_DEVELOP_ERR);
            }
            //校验开发者账号是否有该项目的权限
            Message message = checkServerSDKofProject(appid, projectId);
            if (message != null) {
                return message;
            }
            ProjectPO projectInfo = iSDKProjectServiceClient.getByProjectId(projectId);
            if (projectInfo == null) {
                return new Message(Code.PARAM_ERR, "该项目不存在，请检验数据");
            }

            //解析deviceJson
            SDKEquipSaveDTO addEquip = JSONObject.parseObject(deviceJson, SDKEquipSaveDTO.class);
            if (addEquip == null) {
                return new Message(Code.PARAM_ERR, String.format("%s:%s", "解析deviceJson数据异常", deviceJson));
            }
            addEquip.setProjectId(projectId);
            //设备数据校验
            message = this.checkEquip(addEquip);
            if (message != null) {
                return message;
            }
            String floorNo = addEquip.getFloorNo();
            String floorNoMax = addEquip.getFloorNoMax();
            Integer floorNoNum = (StringUtils.isEmpty(floorNo)) ? 1 : Integer.parseInt(floorNo);
            Integer floorNoMaxNum = (StringUtils.isEmpty(floorNoMax)) ? 99 : Integer.parseInt(floorNoMax);

            String buildIds = addEquip.getBuildIds();
            String unitIds = addEquip.getUnitIds();
            int deviceType = Integer.parseInt(addEquip.getDeviceType());
            String deviceName = addEquip.getDeviceName();
            String deviceStatus = addEquip.getDeviceStatus();

            //默认添加或更新设备的状态是 1-启用
            Integer status = DataStatusEnum.NORMAL.getType();
            if (StringUtils.isNotEmpty(deviceStatus) && deviceStatus.trim().matches("0|1")) {
                //老版本给出的禁用设备是0，需要改为与新版兼容的2
                status = "0".equals(deviceStatus.trim()) ? DataStatusEnum.BAN.getType() : DataStatusEnum.NORMAL.getType();
            }

            int updateCount = 0;
            //将受影响的设备服务地址转化为unitId集合，方便处理
            Set<String> newUnitIdSet = new HashSet<>();
            List<String> unitIdList = iPropertyUnitInfoServiceClient.getUnitIdInBuildIds(buildIds);
            newUnitIdSet.addAll(unitIdList);
            if (StringUtils.isNotEmpty(unitIds)) {
                String[] unitIdArr = unitIds.split(",");
                newUnitIdSet.addAll(Arrays.asList(unitIdArr));
                updateCount = unitIdArr.length;
            }

            //计算本次添加或更新设备服务地址的数量
            if (updateCount == 0 && StringUtils.isNotEmpty(buildIds)) {
                updateCount = buildIds.split(",").length;
            }

            //添加或更新设备
            if (StringUtils.isEmpty(deviceUnique)) {
                //deviceUnique 为空时，添加设备
                //设备名称重复性校验
                PropertyEquipGetDTO deviceNameCheck = new PropertyEquipGetDTO(projectId);
                deviceNameCheck.setDeviceType(deviceType);
                deviceNameCheck.setDeviceName(deviceName);
                List<PropertyEquipGetVO> deviceNameCheckVOS = iPropertyEquipServiceClient.checkEquip(deviceNameCheck);
                if (deviceNameCheckVOS != null && !deviceNameCheckVOS.isEmpty()) {
                    return new Message(Code.PARAM_ERR, "该名称的设备已经存在");
                }

                //查询该类型设备的最大deviceId,拼接deviceUnique
                Map<String, Object> getMaxDeviceId = new HashMap<>();
                getMaxDeviceId.put("projectId", projectId);
                getMaxDeviceId.put("deviceType", deviceType);
                Integer maxDeviceId = iSDKServerEquipServiceClient.getMaxDeviceId(getMaxDeviceId);

                String deviceId = String.format("%04d", maxDeviceId + 1);
                deviceUnique = String.format("%s%d%s", projectId, deviceType, deviceId);
                PropertyCloudIntercomSaveDTO equipAddDTO = new PropertyCloudIntercomSaveDTO(projectId, deviceUnique, deviceType,
                        deviceId, deviceName, floorNoNum, floorNoMaxNum, buildIds, unitIds);

                //地址发生变化，将变化的地址存储的到定时任务表中
                Date date = new Date();
                TimeTaskAuthDeviceAddressInfoPO deviceAddrPO = new TimeTaskAuthDeviceAddressInfoPO(projectId,
                        appid, String.join(",", newUnitIdSet),
                        DataStatusEnum.NORMAL.getType(), date, date);
                equipAddDTO.setTimeTaskAuthDeviceAddressInfoPO(deviceAddrPO);

                iPropertyEquipServiceClient.addPropertyEquip(equipAddDTO);

            } else {
                //解码deviceUnique,更新设备
                deviceUnique = DesEncryptUtil.projectIdDesEncrypt(deviceUnique);
                PropertyEquipGetVO oldEquip = iPropertyEquipServiceClient.getByDeviceUnique(deviceUnique);
                if (oldEquip == null) {
                    return new Message(Code.PARAM_ERR, "所修改的设备唯一码不存在");
                }
                Integer newDeviceType = Integer.parseInt(deviceUnique.substring(8, 9));
                if (deviceType != newDeviceType) {
                    return new Message(Code.PARAM_ERR, "设备类型不能被修改");
                }

                //设备名称重复性校验
                PropertyEquipGetDTO deviceNameCheck = new PropertyEquipGetDTO(projectId);
                deviceNameCheck.setDeviceType(deviceType);
                deviceNameCheck.setDeviceName(deviceName);
                deviceNameCheck.setDeviceUnique(deviceUnique);
                List<PropertyEquipGetVO> deviceNameCheckVOS = iPropertyEquipServiceClient.checkEquip(deviceNameCheck);
                if (deviceNameCheckVOS != null && !deviceNameCheckVOS.isEmpty()) {
                    return new Message(Code.PARAM_ERR, "该名称的设备已经存在");
                }
                PropertyCloudIntercomSaveDTO equipAddDTO = new PropertyCloudIntercomSaveDTO(projectId, deviceUnique, newDeviceType,
                        null, deviceName, floorNoNum, floorNoMaxNum, buildIds, unitIds);
                equipAddDTO.setStatus(status);
                //找到变化后地址转化为unitId的差集
                Set<String> oldUnitIdSet = this.getEquipOldUnitId(deviceUnique);
                Set<String> diffUnitIdSet = SetListUtil.getDiffSet(newUnitIdSet, oldUnitIdSet);

                //电梯的楼层发生变化也需要找到电梯地址，并通知权限同步
                if ((PropertyEquipTypeEnum.ELEVATOR.getType() == deviceType) && (oldEquip.getFloorNo()) != (equipAddDTO.getFloorNo())) {
                    diffUnitIdSet.addAll(newUnitIdSet);
                }
                if (!diffUnitIdSet.isEmpty()) {
                    Date date = new Date();
                    TimeTaskAuthDeviceAddressInfoPO deviceAddrPO = new TimeTaskAuthDeviceAddressInfoPO(projectId,
                            appid, String.join(",", diffUnitIdSet),
                            DataStatusEnum.NORMAL.getType(), date, date);
                    equipAddDTO.setTimeTaskAuthDeviceAddressInfoPO(deviceAddrPO);
                } else {
                    //如果设备地址未发生变化，但设备名称发生变化，需要查找所影响的住户，并刷新缓存
                    if (!deviceName.equals(oldEquip.getDeviceName())) {
                        equipAddDTO.setIsOnlyNameChange(true);
                    } else {
                        equipAddDTO.setIsOnlyNameChange(false);
                    }
                }
                iPropertyEquipServiceClient.updatePropertyEquip(equipAddDTO);
            }

            JSONObject js = new JSONObject();
            js.put("deviceUnique", DesEncryptUtil.projectIdEncrypt(deviceUnique, null, null));
            js.put("deviceName", deviceName);
            js.put("deviceAddr", updateCount);
            return new Message(Code.SUCCESS, js);
        } catch (Exception e) {
            throw new DataAccessException("【SDK服务端_SDKServerOldEquipController_sdkAddDevice】添加更新设备异常", e);
        }
    }


    /**
     * <desc>
     *      根据地址，分页获取SDK的设备信息
     * <desc/>
     * @param   appid   应用接入凭证
     * @param   sdkEquipGetPageDTO   查询参数
     * @return    Message   旧版标准格式化响应结果
     * @author Biao.R
     * @createDate 2017/11/16
     */
    @ResponseBody
    @RequestMapping("/app/getDeviceInfo")
    public Message getDeviceInfoByProjectId(String appid, SDKEquipGetPageDTO sdkEquipGetPageDTO) {
        try {
            String desProjectId = DesEncryptUtil.projectIdDesEncrypt(sdkEquipGetPageDTO.getProjectId());
            if (desProjectId == null  ) {
                return new Message(Code.APP_DEVELOP_ERR);
            }
            //校验开发者账号是否有该项目的权限
            Message message = checkServerSDKofProject(appid, desProjectId);
            if (message != null) {
                return message;
            }

            String buildNum = sdkEquipGetPageDTO.getBuildNum();
            String unitNum = sdkEquipGetPageDTO.getUnitNum();
            if (StringUtils.isEmpty(buildNum) && StringUtils.isNotEmpty(unitNum)) {
                return new Message(Code.PARAM_ERR);
            }
            JSONArray deviceArr = new JSONArray();

            //根据地址，分页查询设备
            sdkEquipGetPageDTO.setProjectId(desProjectId);
            Map<String, String> param = new HashMap<>();
            param.put("projectId", desProjectId);
            param.put("buildNumIn", buildNum);
            String buildIds = iSDKBuildServiceClient.getBuildIdsByParams(param);
            if (StringUtils.isNotEmpty(buildIds)) {
                String[] buildIdArr = buildIds.split(",");
                sdkEquipGetPageDTO.setBuildId(String.join(",", buildIdArr));
                if (StringUtils.isNotEmpty(unitNum)) {
                    param.put("buildId", buildIdArr[0]);
                    param.put("unitNumIn", unitNum);
                    String unitIds = iSDKServerOldUnitServiceClient.getUnitIdsByParams(param);
                    if(StringUtils.isNotEmpty(unitIds)){
                    sdkEquipGetPageDTO.setUnitId(unitIds);
                    }
                }
            }
            Page<PropertyEquipGetDTO> page = iSDKServerEquipServiceClient.getEquipPageByAddr(sdkEquipGetPageDTO);
            List<PropertyEquipGetDTO> deviceInfoList = page.getRows();
            if (null != deviceInfoList && !deviceInfoList.isEmpty()) {
                for (PropertyEquipGetDTO deviceInfo : deviceInfoList) {
                    Set<String> buildNumSet = new HashSet<>();
                    Set<String> unitNumSet = new HashSet<>();
                    JSONObject js = new JSONObject();
                    String deviceUnique = deviceInfo.getDeviceUnique();
                    js.put("deviceUnique", DesEncryptUtil.projectIdEncrypt(deviceUnique, null, null));
                    js.put("deviceName", deviceInfo.getDeviceName());
                    js.put("deviceType", deviceInfo.getDeviceType());

                    //通过设备查询设备归属地编号
                    List<SDKUnitInfoSaveDTO> equipAddrNumDTOS = iSDKServerEquipServiceClient.getAddrNumByDeviceUnique(deviceUnique);
                    if (equipAddrNumDTOS != null && !equipAddrNumDTOS.isEmpty()) {
                        for (SDKUnitInfoSaveDTO equipAddrNum : equipAddrNumDTOS) {
                            buildNumSet.add(equipAddrNum.getBuildNum());
                            String equipUnitNum = equipAddrNum.getUnitNum();
                            if (StringUtils.isNotEmpty(equipUnitNum)) {
                                unitNumSet.add(equipUnitNum);
                            }
                        }
                    }
                    js.put("buildNum", String.join(",", buildNumSet));
                    js.put("unitNum", String.join(",", unitNumSet));
                    deviceArr.add(js);
                }
            }
            JSONObject js = new JSONObject();
            js.put("deviceArr", deviceArr);
            js.put("totalNumber", page.getPage().getTotalNumber());
            return new Message(Code.SUCCESS, js);
        } catch (Exception e) {
            throw new DataAccessException("【SDK服务端_SDKServerOldEquipController_getDeviceInfoByProjectId】分页查询设备信息异常", e);
        }
    }


    /**
     * <desc>
     *      校验开发者账号是否有该项目的权限
     * <desc/>
     * @param   appid     应用接入凭证
     * @param    projectId     独立开发者所管辖的项目Id
     * @return  Message   旧版标准格式化响应结果
     * @author Biao.R
     * @createDate 2017/11/6
     */
    private Message checkServerSDKofProject(String appid, String projectId) {
        PropertyRoleProjectPO roleProjectPO = new PropertyRoleProjectPO(projectId, PropertyPublicRoleEnum.DEVELOPER.getRoleId(), appid);
        List<PropertyRoleProjectPO> roleProjectPOS = iSDKUserServiceClient.getRoleProject(roleProjectPO);
        if (roleProjectPOS != null && roleProjectPOS.isEmpty()) {
            return new Message(Code.NOT_PERMISSION);
        }
        return null;
    }


    /**
     * <desc>
     *      查询通行记录
     * <desc/>
     * @param   sdkEquipGetByPageDTO    查询参数DTO
     * @return
     * @author Biao.R
     * @createDate 2017/11/21
     */
    @ResponseBody
    @RequestMapping("/server/deviceRecord/list")
    public Message getDeviceRecord(SDKEquipGetByPageDTO sdkEquipGetByPageDTO) {
        try {
            JSONObject js = new JSONObject();
            String appid = sdkEquipGetByPageDTO.getAppid();
            String desProjectId = DesEncryptUtil.projectIdDesEncrypt(sdkEquipGetByPageDTO.getProjectId());

            if (desProjectId == null  ) {
                return new Message(Code.APP_DEVELOP_ERR);
            }
            //校验开发者账号是否有该项目的权限
            Message message = checkServerSDKofProject(appid, desProjectId);
            if (message != null) {
                return message;
            }
            sdkEquipGetByPageDTO.setDeviceUnique(DesEncryptUtil.projectIdDesEncrypt(sdkEquipGetByPageDTO.getDeviceUnique()));
            sdkEquipGetByPageDTO.setProjectId(desProjectId);
            Page<PropertyEquipRecordGetVO> recordPage = iSDKServerEquipServiceClient.getEquipRecordPage(sdkEquipGetByPageDTO);

            List<PropertyEquipRecordGetVO> deviceRecords =recordPage.getRows();
            List<Map<String,String>> recordList = new ArrayList<>();
            if(null!=deviceRecords && !deviceRecords.isEmpty()){
                for (PropertyEquipRecordGetVO deviceRecord : deviceRecords) {
                    Map<String,String> resultMap = new HashMap<>();
                    resultMap.put("userName",deviceRecord.getRealName());
                    resultMap.put("userPhone",deviceRecord.getPhoneNum());
                    resultMap.put("deviceUnique",DesEncryptUtil.projectIdEncrypt(deviceRecord.getDeviceUnique(),null,null));
                    resultMap.put("deviceName",deviceRecord.getDeviceName());
                    resultMap.put("openType",deviceRecord.getOpenTypeName());
                    resultMap.put("passTime",deviceRecord.getPassTime());
                    recordList.add(resultMap);
                }
            }
            js.put("recordsNum",recordPage.getPage().getTotalNumber());
            js.put("recordsData",recordList);
            return new Message(Code.SUCCESS,js);
        } catch (Exception e) {
            throw new DataAccessException("【SDK服务端_SDKServerOldEquipController_getDeviceRecord】分页查询通行记录异常", e);
        }
    }
    
    
    /**
     * <desc>
     *      添加设备前的参数校验
     * <desc/>
     * @param   addEquip  添加的设备实体
     * @return Message   旧版标准格式化响应结果
     * @author Biao.R
     * @createDate 2017/11/27
     */
    public Message checkEquip(SDKEquipSaveDTO addEquip) {
        String deviceType = addEquip.getDeviceType();

        if (!deviceType.matches("1|2") || StringUtils.isEmpty(addEquip.getDeviceName())) {
            return new Message(Code.PARAM_ERR, "设备类型或设备名称非法");
        }

        String buildNum = addEquip.getBuildNum();
        String unitNum = addEquip.getUnitNum();
        if (StringUtils.isEmpty(buildNum)) {
            return new Message(Code.PARAM_ERR, String.format("%s:%s", "至少添加一个楼栋地址", buildNum));
        }

        //根据楼栋单元编号获取楼栋单元ID
        Map<String, String> params = new HashMap<>();
        params.put("projectId", addEquip.getProjectId());
        params.put("buildNumIn", buildNum);
        String buildIds = iSDKBuildServiceClient.getBuildIdsByParams(params);
        String unitIds ="";
        if (StringUtils.isEmpty(buildIds)) {
            return new Message(Code.PARAM_ERR, "请检查设备类型和楼栋单元对应数目");
        }
        String[] buildIdArr = buildIds.split(",");

        if (StringUtils.isNotEmpty(unitNum)) {
            Map<String, String> param = new HashMap<>();
            param.put("buildId", buildIdArr[0]);
            param.put("unitNumIn", unitNum);
            unitIds = iSDKServerOldUnitServiceClient.getUnitIdsByParams(param);
            if (StringUtils.isNotEmpty(unitIds)) {
                addEquip.setUnitIds(unitIds);
            }
        }

        //有单元的时候，楼栋必须为1栋
        if (StringUtils.isNotEmpty(unitIds) && buildIdArr.length != 1) {
            return new Message(Code.PARAM_ERR, "请检查设备类型和楼栋单元对应数目");
        }
        if (PropertyEquipTypeEnum.ELEVATOR.getType() == Integer.parseInt(deviceType)) {

            //如果是电梯,要加上楼层范围
            String floorNo = addEquip.getFloorNo();
            String floorNoMax = addEquip.getFloorNoMax();
            Integer floorNoNum = (StringUtils.isEmpty(floorNo)) ? 1 : Integer.parseInt(floorNo);
            Integer floorNoMaxNum = (StringUtils.isEmpty(floorNoMax)) ? 99 : Integer.parseInt(floorNoMax);
            if (floorNoNum > floorNoMaxNum || floorNoNum < 1 || floorNoMaxNum > 99) {
                return new Message(Code.PARAM_ERR, "电梯层数必须在1~99之间" + "floorNo:" + floorNo + ",floorNoMax:" + floorNoMax);
            }
        }
        addEquip.setBuildIds(String.join(",",buildIdArr));
        return null;
    }


    /**
     * <desc>
     *      根据设备唯一码查询设备归属地址，并精确到unitId
     * <desc/>
     * @param   deviceUnique    待查询的设备唯一码
     * @return 归属地精确到小区后的unitId集合
     * @author Biao.R
     * @createDate 2017/12/15
     */
    private Set<String> getEquipOldUnitId(String deviceUnique) {
        List<PropertyEquipAddressVO> oldEquipAddress = iPropertyEquipServiceClient.getEquipAddress(deviceUnique);
        Set<String> oldUnitIdSet = new HashSet<>();
        if (oldEquipAddress != null && !oldEquipAddress.isEmpty()) {
            List<String> oldBuildIdList = new ArrayList<>();
            oldEquipAddress.forEach(oldAdress -> {
                List<PropertyProjectUnitSimpleInfoVO> units = oldAdress.getUnits();
                if (units != null && !units.isEmpty()) {
                    for (PropertyProjectUnitSimpleInfoVO unit : units) {
                        oldUnitIdSet.add(unit.getUnitId());
                    }
                } else {
                    oldBuildIdList.add(oldAdress.getBuildId());
                }
            });
            if (!oldBuildIdList.isEmpty()) {
                oldUnitIdSet.addAll(iPropertyUnitInfoServiceClient.getUnitIdInBuildIds(String.join(",", oldBuildIdList)));
            }
        }
        return oldUnitIdSet;
    }
}